import * as XLSX from "xlsx/xlsx.mjs";

//转化excel
function readFile(file) {
  //文件读取
  return new Promise((resolve) => {
    let reader = new FileReader();
    reader.readAsBinaryString(file); //以二进制的方式读取
    reader.onload = (ev) => {
      resolve(ev.target.result);
    };
  });
}

function toExcel(dom) {
  XLSX.utils.table_to_book(document.getElementById("dataTable"));
}

const ExcelToJson = async (ev) => {
  let file = ev.raw;
  console.log(file);
  if (!file) {
    console.log("文件打开失败");
    return;
  } else {
    let data = await readFile(file);
    let workbook = XLSX.read(data, { type: "binary" }); //解析二进制格式数据
    let worksheet = workbook.Sheets[workbook.SheetNames[0]]; //获取第一个Sheet
    let result = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); //json数据格式
    return result;
  }
};

export default { ExcelToJson };
